package com.dycx.food.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.food.mapper.SpecFoodMapper;
import com.dycx.food.dto.SpecDTO;
import com.dycx.food.entity.GoodsClassDO;
import com.dycx.food.entity.SpecDO;
import com.dycx.food.enums.SpecTypeEnum;
import com.dycx.food.service.GoodsClassFoodService;
import com.dycx.food.service.SpecFoodService;
import com.dycx.food.service.SpecValueFoodService;
import com.dycx.food.vo.SpecSimpleVO;
import com.dycx.food.vo.SpecVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class SpecFoodImpl extends ServiceImpl<SpecFoodMapper, SpecDO> implements SpecFoodService {

    private SpecFoodMapper specMapper;
    private GoodsClassFoodService goodsClassService;
    private SpecValueFoodService specValueService;

    @Override
    public Boolean delete(int specId) {
        return this.removeById(specId);
    }

    /**
     * 添加
     *
     * @param specDto
     * @return
     */
    @Override
    public Boolean insert(SpecDTO specDto) {
        SpecDO specDO = BeanUtil.toBean(specDto, SpecDO.class);
        specDto.setSpecId(specDO.getSpecId());
        return this.save(specDO);
    }

    /**
     * 添加/修改
     *
     * @param specDto
     * @return
     */
    @Override
    public Boolean update(SpecDTO specDto) {
        SpecDO specDO = BeanUtil.toBean(specDto, SpecDO.class);
        return this.updateById(specDO);
    }

    /**
     * 更新排序
     *
     * @param specDto
     * @return
     */
    @Override
    public Boolean updateSort(SpecDTO specDto) {
        SpecDO spec = new SpecDO();
        spec.setSpecId(specDto.getSpecId());
        spec.setSort(specDto.getSort());
        return this.updateById(spec);
    }


    /**
     * 获取父角色下的所有角色
     *
     * @return
     */
    @Override
    public Page<SpecVO> getPage(SpecDTO specDto) {
        Page<SpecVO> page = new Page<>(specDto.getPageIndex(), specDto.getPageSize());  // 查询第1页，每页返回5条
        Page<SpecVO> pageData = specMapper.getPage(page, specDto);
        List<SpecVO> dataList = pageData.getRecords();
        for (SpecVO specVo : dataList) {
            SpecTypeEnum specTypeEnum = SpecTypeEnum.getCode(specVo.getSpecType());
            assert specTypeEnum != null;
            specVo.setSpecTypeName(specTypeEnum.getName());
            specVo.setSpecTypeStyle(specTypeEnum.getStyle());
        }

        pageData.setRecords(dataList);

        return pageData;
    }

    /**
     * 获取商品分类下所有规格
     *
     * @param goodsClassId
     * @return
     */
    public List<SpecSimpleVO> getSpecByGoodsClassId(int goodsClassId) {
        if (ObjectUtil.isEmpty(goodsClassId) && goodsClassId == 0) {
            throw new RuntimeException("商品分类不能为空");
        }

        GoodsClassDO goodsClassDO = goodsClassService.getById(goodsClassId);

        LambdaQueryWrapper<SpecDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpecDO::getGoodsTypeId, goodsClassDO.getGoodsTypeId());
        queryWrapper.orderByDesc(SpecDO::getSort);
        List<SpecDO> specList = this.list(queryWrapper);

        if (ObjectUtil.isEmpty(specList)) return null;

        List<SpecSimpleVO> specVoList = specList.stream().map(item ->
                BeanUtil.toBean(item, SpecSimpleVO.class)
        ).collect(Collectors.toList());

        specVoList.forEach(item -> {
            item.setSpecValueList(specValueService.getAllListBySpecId(item.getSpecId()));
        });

        return specVoList;
    }

    /**
     * 获取商品分类下所有检索规格
     *
     * @param goodsClassId
     * @return
     */
    @Override
    public List<SpecSimpleVO> getFilterSpecByClassId(int goodsClassId) {
        if (ObjectUtil.isEmpty(goodsClassId) && goodsClassId == 0) {
            throw new RuntimeException("商品分类不能为空");
        }

        GoodsClassDO goodsClassDO = goodsClassService.getById(goodsClassId);

        LambdaQueryWrapper<SpecDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpecDO::getGoodsTypeId, goodsClassDO.getGoodsTypeId());
        queryWrapper.eq(SpecDO::getFilterCondition, true);
        queryWrapper.orderByDesc(SpecDO::getSort);
        List<SpecDO> specList = this.list(queryWrapper);

        if (ObjectUtil.isEmpty(specList)) return null;

        List<SpecSimpleVO> specVoList = specList.stream().map(item ->
                BeanUtil.toBean(item, SpecSimpleVO.class)
        ).collect(Collectors.toList());

        specVoList.forEach(item -> {
            item.setSpecValueList(specValueService.getAllListBySpecId(item.getSpecId()));
        });

        return specVoList;
    }
}
